<?php

class ProjectDao extends Dao {
	
	function __construct() {
        parent::__construct();
        $this->table = 'project';
    }

    public function insertObject($obj){
        $result = DB::insert(DB::raw("INSERT INTO project (name, description, start_date, due_date, status_id,created_by, created_date) ".
                "VALUES ('".$obj->getName()."' ".
                        ", '".$obj->getDescription()."' ".
                        ", '".$obj->getStartDate()."' ".
                        ", '".$obj->getDueDate()."' ".
                        ", '".$obj->getStatus()->getId()."' ".
                        ", '".$obj->getCreatedBy()->getId()."' ".
                        ", null ".
                        ")"));
        if (!$result) { return null; }

        return DB::getPdo()->lastInsertId();
    }

    public function updateObject($obj, $old_id){
        DB::update(DB::raw("UPDATE project SET id='".$obj->getId()."',name='".$obj->getName()."' ".
                        ",description='".$obj->getDescription()."' ".
                        ", start_date='".$obj->getStartDate()."' ".
                        ", due_date='".$obj->getDueDate()."' ".
                        ", status_id='".$obj->getStatus()->getId()."' ".
                        ", last_updated_by='".$obj->getUpdatedBy()->getId()."' ".
                        ", last_updated_date= null ".
                        " WHERE id=".$old_id." "));

        return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM project where id='.$id.'"));
        
        return true;
    }
    
    public function getObject($id){
        $hasil = DB::select(DB::raw("SELECT * FROM project where id='".$id."'"));

        if (!$hasil) { return null; }
        return $this->toObject($hasil[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM project"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
            
        return $list;
    }
    
    protected function toObject($rowdata=null){
        $obj = new Project();
        $obj->setId($rowdata->id);
        $obj->setName($rowdata->name);
        $obj->setDescription($rowdata->description);
        $obj->setStartDate($rowdata->start_date);
        $obj->setDueDate($rowdata->due_date);
		
            $user = new User();
            $user->setId($rowdata->created_by);
            $user->setIsLoaded(false);
            $obj->setCreatedBy($user);
                
        $obj->setCreatedDate($rowdata->created_date);
        
            $userlast = new User();
            $userlast->setId($rowdata->last_updated_by);
            $userlast->setIsLoaded(false);
            $obj->setUpdatedBy($userlast);
                
        $obj->setUpdatedDate($rowdata->last_updated_date);

        return $obj;
    }
	
    
}

   